package com.papper.mng.controller.biz;

import com.google.common.collect.Maps;
import com.papper.common.bean.biz.BizFavorContacts;
import com.papper.common.bean.biz.BizFavorGroup;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.biz.BizFavorContactsService;
import com.papper.common.service.biz.BizFavorGroupService;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-04-21 11:04:21
 * @Description:个人人情往来
 */
@Controller
@RequestMapping("/biz/favor/contacts")
public class BizFavorContactsController extends BaseAction {

    @Autowired
    BizFavorContactsService bizFavorContactsService;
    @Autowired
    BizFavorGroupService    bizFavorGroupService;

    /**
     * 个人人情往来
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model, Long groupId) {
        // 获取组信息
        List<BizFavorGroup> groupList = bizFavorGroupService.findAll();
        model.addAttribute("groupList", groupList);
        if (groupList == null) {
            return render("biz/favor/contacts/index");
        }
        BizFavorGroup favorGroup = bizFavorGroupService.findById(groupId);
        if (favorGroup == null) {
            favorGroup = groupList.get(0);
            groupId = favorGroup.getId();
        }
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BizFavorContacts.class);
        modelParm(objects, model);
        objects.put("groupId", groupId);
        model.addAttribute("groupId", groupId);
        model.addAttribute("favorGroup", favorGroup);
        // 数据查询组装
        Example                example  = buildExample(objects, model, BizFavorContacts.class);
        List<BizFavorContacts> infoList = bizFavorContactsService.find(example);
        model.addAttribute("records", infoList);
        return render("biz/favor/contacts/index");
    }

    /**
     * 个人人情往来_新增
     */
    @GetMapping(value = "/add")
    public String add_get(Model model, Long groupId) {
        model.addAttribute("groupId", groupId);
        return render("biz/favor/contacts/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BizFavorContacts bizFavorContacts) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        // 添加个人人情往来
        try {
            bizFavorContacts.setDataStatus(DataStatusEnum.OPEN.getCode());
            bizFavorContacts.setCreateTime(new Date());
            bizFavorContacts.setModifyTime(new Date());
            bizFavorContactsService.save(bizFavorContacts);
            return new DwzResponse("200", "个人人情往来添加成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人人情往来添加失败:" + e.getMessage() + "!!!");
        }
    }

    /**
     * 个人人情往来_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BizFavorContacts entity = bizFavorContactsService.findById(id);
        model.addAttribute("entity", entity);
        return render("biz/favor/contacts/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BizFavorContacts bizFavorContacts) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (bizFavorContacts == null || bizFavorContacts.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            bizFavorContacts.setModifyTime(new Date());
            bizFavorContactsService.update(bizFavorContacts);
            return new DwzResponse("200", "个人人情往来修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人人情往来修改失败:" + e.getMessage());
        }
    }

    /**
     * 个人人情往来_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || 0 >= id.intValue()) {
            return new DwzResponse("300", "参数ID为空！");
        }
        BizFavorContacts old = bizFavorContactsService.findById(id);
        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            bizFavorContactsService.deleteById(id);
            return new DwzResponse("200", "个人人情往来删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人人情往来删除失败:" + e.getMessage());
        }
    }

    /**
     * 个人人情往来_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BizFavorContacts bizFavorContacts = bizFavorContactsService.findById(id);
        model.addAttribute("entity", bizFavorContacts);
        return render("biz/favor/contacts/detail");
    }

}
